package main.java.study.websocket.utils;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

public class IDUtils {


    private static boolean tmpIDlocked = false;

    /**
     * 图片名生成
     */
    public static String genImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        //如果不足三位前面补0
        String str = millis + String.format("%03d", end3);

        return str;
    }


    /**
     * 商品id生成
     */
    public static String getTimeId() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上两位随机数
        Random random = new Random();
        int end2 = random.nextInt(99);
        //如果不足两位前面补0
        String str = millis + String.format("%02d", end2);
        long id = new Long(str);
        String timeId = Long.toString(id);
        return timeId;
    }

    /**
     * 生成UUID工具类
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }

    /**
     * 生成UUID工具类
     */
    public static String getUUIDFileName(String profix) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return StringUtils.isEmpty(profix) ? uuid + ".undefine" : uuid + "." + profix;
    }


    //    生成时间+随机数的id
    public static String getTimeId(String pre, Integer needLength) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String part2 = format.format(new Date());

        int originLength = pre.length() + part2.length();
        if (needLength == null || needLength <= originLength) {
            needLength = pre.length() + part2.length();
        }
        StringBuffer part3 = new StringBuffer();
        Random random = new Random();

        for (int i = 0; i < needLength - originLength; i++) {
            int rand = random.nextInt(10);
            part3.append(rand);
        }
        return pre + "" + part2 + "" + part3;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 500; i++) {
            System.out.println(getLongId());
        }

    }

    public static String genSMSCode() {
        String value = "";
        Random random = new Random();
        int gen = random.nextInt(2);
        String charOrNum = gen % 2 == 0 ? "char" : "num";
        if ("char".equals(charOrNum)) {
            //字符
            int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
            int ascii = random.nextInt(26);
            value += (char) (ascii + temp);
        } else if ("num".equals(charOrNum)) {
            //是数字
            value += String.valueOf(random.nextInt(10));
        }
        return value;
    }

    public static String genSMSCode_num() {
        String value = "";
        Random random = new Random();
        while (true) {
            value += String.valueOf(random.nextInt(10));
            if (value.length() == 6) {
                break;
            }
        }
        return value;

    }

    //生成随机数字和字母,
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();
        //length为几位密码
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static String genImageName(String fileName) {
        return getTimeId() + getSuffix(fileName);
    }

    public static String getSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    public static String genOssKey(String originalFilename) {
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        return getUUID() + suffix;
    }


    public static Long getLongId() {
        String ltime;
        if (tmpIDlocked == false) {
            tmpIDlocked = true;
            //当前：（年、月、日、时、分、秒、毫秒）
            ltime = new SimpleDateFormat("yyMMddhhmmssSSS").format(new Date());
            StringBuffer part3 = new StringBuffer();
            Random random = new Random();
            part3.append(ltime);
            for (int i = 0; i < 4; i++) {
                int rand = random.nextInt(9);
                part3.append(rand);
            }
            tmpIDlocked = false;
            return new Long(part3.toString());
        } else {
            return null;
        }


    }


}